﻿using System;
using System.Windows.Input;

namespace HashCalculator.Commands
{
    abstract class DelegateCommandBase : ICommand
    {
        Action<object> execute;

        Func<object, bool> canExecute;

        public DelegateCommandBase(Action<object> execute) : this(execute, o => true) { }

        public DelegateCommandBase(Action<object> execute, Func<object, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        protected void Execute(object parameter)
        {
            execute(parameter);
        }

        protected bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        bool ICommand.CanExecute(object parameter)
        {
            return CanExecute(parameter);
        }

        void ICommand.Execute(object parameter)
        {
            Execute(parameter);
        }
    }
}
